# -*- coding: utf-8 -*- 
# @project : 《剑指offer》
# @Author : created by bensonrachel on 2021/6/22
# @File : 链表中环的入口结点.py


# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):  叫构造函数!
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, head):
        # write code here
        #双指针版本
        slow = head
        fast = head
        while(fast):#慢一步，快两步
            slow = slow.next
            fast = fast.next
            if(fast):fast=fast.next
            if(not fast):return None
            #第一次碰头
            if(fast == slow):
                slow = head
                if(slow==fast):return slow
                while(1):#慢一步，快一步
                    slow = slow.next
                    fast = fast.next
                    if(fast == slow):#第二次碰头
                        return fast
        return None